﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace hello.csharp.winform
{
    public partial class Form1 : Form
    {
        private const string DOCK_PANEL_CONFIG = "dockpanel3.config";
        private DockPanel _dockPanel;

        private void InitializeDockPanel()
        {
            SuspendLayout();
            _dockPanel = new DockPanel
            {
                DocumentStyle = DocumentStyle.DockingWindow,
                Theme = new VS2015BlueTheme(),
                Dock = DockStyle.Fill
            };
            _toolStripContainer.ContentPanel.Controls.Add(_dockPanel);
            _dockPanel.BringToFront();
            DockPanelLoadFromXml();
            PerformLayout();
            ResumeLayout(false);
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            DockPanelSaveAsXml();
            base.OnClosing(e);
        }

        /// <summary>
        ///     控件提供了一个保存布局状态的方法，它默认是没有保存的。
        /// </summary>
        private void DockPanelSaveAsXml()
        {
            _dockPanel.SaveAsXml(GetLayoutConfigFile());
        }

        /// <summary>
        /// 主界面启动通过保存的布局配置文件加载历史(上一次使用软件的最终布局)布局
        /// </summary>
        private void DockPanelLoadFromXml()
        {
            var deserializeDockContent = new DeserializeDockContent(GetViewFromPersistString);
            var configFile = GetLayoutConfigFile();
            if (File.Exists(configFile))
                _dockPanel.LoadFromXml(configFile, deserializeDockContent);
        }

        private IDockContent GetViewFromPersistString(string persist)
        {
            // if (persist == typeof(LoggerView).ToString())
            // {
            //     return _loggerView;
            // }
            return null;
        }

        private static string GetLayoutConfigFile()
        {
            var dir = Path.GetDirectoryName(Application.ExecutablePath);
            return dir != null ? Path.Combine(dir, DOCK_PANEL_CONFIG) : DOCK_PANEL_CONFIG;
        }

    }
}
